package com.yanxinim.cn.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanxinim.cn.R;
import com.yanxinim.cn.bean.redpacket.BankCardBean;
import com.yanxinim.cn.call.CustomOnItemClickListener;

/**
 * @ClassName BankCardAdapter
 * @Description 银行卡列表适配器
 * @Author ChenXL
 * @Date 2021/5/25 19:57
 * @Version 1.0
 */
public class BankCardAdapter extends BaseListAdapter{

    LayoutInflater mInflater;

    CustomOnItemClickListener onItemClickListener;

    public BankCardAdapter(Context ctx) {
        super(ctx);
        mInflater = LayoutInflater.from(ctx);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_bank_card, parent, false);
            holder = new ViewHolder();
            holder.ivBankLogo = (ImageView) convertView.findViewById(R.id.ivBankLogo);
            holder.tvBankName = (TextView) convertView.findViewById(R.id.tvBankName);
            holder.tvRemoveBind = (TextView) convertView.findViewById(R.id.tvRemoveBind);
            holder.tvCardNo = (TextView) convertView.findViewById(R.id.tvCardNo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String cardNo = ((BankCardBean)datas.get(position)).getBankCardNo();
        String cardNoStr = "····  ····  ····  " + cardNo.substring(cardNo.length() - 4, cardNo.length());
        holder.ivBankLogo.setImageResource(getBankLogo(((BankCardBean)datas.get(position)).getBankCode()));
        holder.tvBankName.setText(((BankCardBean)datas.get(position)).getBankName());
        holder.tvCardNo.setText(cardNoStr);
        holder.tvRemoveBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null){
                    v.setTag(datas.get(position));
                    onItemClickListener.onItemClick(v, position);
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView ivBankLogo;
        TextView tvBankName;
        TextView tvRemoveBind;
        TextView tvCardNo;
    }

    public void setOnItemClickListener(CustomOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 获取银行logo
     */
    public static int getBankLogo(String bankCode){
        int imageId = 0;
        switch (bankCode){
            case "CEB":
                imageId = R.drawable.bank_logo_white_ceb;
                break;
            case "PSBC":
                imageId = R.drawable.bank_logo_white_psbc;
                break;
            case "ABC":
                imageId = R.drawable.bank_logo_white_abc;
                break;
            case "SHYH":
                imageId = R.drawable.bank_logo_white_shyh;
                break;
            case "CCB":
                imageId = R.drawable.bank_logo_white_ccb;
                break;
            case "BOCO":
                imageId = R.drawable.bank_logo_white_boco;
                break;
            case "SDB":
                imageId = R.drawable.bank_logo_white_sdb;
                break;
            case "HXB":
                imageId = R.drawable.bank_logo_white_hxb;
                break;
            case "CMBC":
                imageId = R.drawable.bank_logo_white_cmbc;
                break;
            case "BOC":
                imageId = R.drawable.bank_logo_white_boc;
                break;
            case "ICBC":
                imageId = R.drawable.bank_logo_white_icbc;
                break;
            case "CITIC":
                imageId = R.drawable.bank_logo_white_citic;
                break;
            case "SPDB":
                imageId = R.drawable.bank_logo_white_spdb;
                break;
            case "CIB":
                imageId = R.drawable.bank_logo_white_cib;
                break;
            case "CGB":
                imageId = R.drawable.bank_logo_white_cgb;
                break;
            case "CMBCHINA":
                imageId = R.drawable.bank_logo_white_cmbchina;
                break;
            case "BCCB":
                imageId = R.drawable.bank_logo_white_bccb;
                break;
            default:
                break;
        }
        return imageId;
    }
}
